const express = require('express');
const router = express.Router();
const sql = require("../sql/index");
const Pro = require("../sql/collection/Pro");

/**
* @api {get} /api/pro 请求商品列表数据
* @apiName GetApiPro
* @apiGroup Pro
* 
* @apiParam { Number } limit 每页显示的数据 默认值为10
* @apiParam { Number } count 页码 默认值为1
*   
* @apiSuccessExample { json } Success-Response:
    res.send({
            code: "200",
            message: "商品列表接口",
            data
        })
* @apiSampleRequest /api/pro
* @apiVersion 0.0.0
*/
// 商品列表数据
router.get('/', function (req, res, next) {
    let { limit, count } = req.query;
    limit = limit * 1 || 10; // 每页显示的商品数，默认值10
    count = count * 1 || 1; // 页码，默认值1
    sql.page(Pro, {}, { _id: 0 }, limit, count).then(data => {
        res.send({
            code: "200",
            message: "商品列表接口",
            data
        });
    })
});

/**
* @api {get} /api/pro/details 请求商品详情数据
* @apiName GetApiProDetails
* @apiGroup Pro
*
* @apiParam { String } proid 商品的ID
* 
* @apiSuccessExample { json } Success-Response:
    res.send({
            code: "10001",
            message: "没有此商品"
        })
    res.send({
            code: "200",
            message: "商品详情接口",
            data
        })
* @apiSampleRequest /api/pro/details
* @apiVersion 0.0.0
*/
// 商品详情
router.get('/details', function (req, res, next) {
    let { proid } = req.query;
    sql.find(Pro, { proid }, { _id: 0 }).then((data) => {
        if (data.length === 0) {
            res.send({
                code: "10001",
                message: "没有此商品"
            })
        } else {
            res.send({
                code: "200",
                message: "商品详情接口",
                data
            });
        }
    });
});

/**
* @api {get} /api/pro/kind 商品分类数据
* @apiName GetApiProKind
* @apiGroup Pro
* 
* @apiSuccessExample { json } Success-Response:
    res.send({
            code: "200",
            message: "商品分类数据",
            data
        })
* @apiSampleRequest /api/pro/kind
* @apiVersion 0.0.0
*/
// 商品分类数据
router.get("/kind", (req, res, next) => {
    sql.distinct(Pro, "prokind").then((data) => {
        res.send({
            code: "200",
            message: "商品分类数据",
            data
        })
    })
})
/**
* @api {get} /api/pro/brand 商品品牌数据
* @apiName GetApiProBrand
* @apiGroup Pro
* 
* @apiParam { String } prokind 商品种类
*
* @apiSuccessExample { json } Success-Response:
    res.send({
            code: "200",
            message: "商品品牌数据",
            data
        })
* @apiSampleRequest /api/pro/brand
* @apiVersion 0.0.0
*/
// 商品品牌数据
router.get("/brand", (req, res, next) => {
    const { prokind } = req.query
    sql.find(Pro, { prokind }, { _id: 0, proid: 1, brand: 1, logo: 1 }).then(result => {
        const obj = {}
        const data = result.reduce((prev, val) => {
            obj[val.brand] ? '' : obj[val.brand] = true && prev.push(val)
            return prev
        }, [])
        res.send({
            code: "200",
            message: "商品品牌数据",
            data
        })
    })
})
/**
* @api {get} /api/pro/brandlist 商品品牌列表数据
* @apiName GetApiProBrandList
* @apiGroup Pro
*
* @apiParam { String } prokind 商品种类
*
* @apiSuccessExample { json } Success-Response:
    res.send({
            code: "200",
            message: "商品品牌列表数据",
            data
        })
* @apiSampleRequest /api/pro/brandlist
* @apiVersion 0.0.0
*/
// 商品品牌列表数据
router.get("/brandlist", (req, res, next) => {
    const { prokind, brand } = req.query
    sql.find(Pro, { prokind, brand }, { _id: 0 }).then(data => {
        res.send({
            code: "200",
            message: "商品品牌列表数据",
            data
        })
    })
})
module.exports = router;
